package com.ncmmall.mall.controller.business.marketing;

import com.ncmmall.application.business.marketing.GiftApplication;
import com.ncmmall.application.securitys.logs.OperationLogApplication;
import com.ncmmall.domain.business.marketing.gift.GiftActivity;
import com.ncmmall.domain.business.marketing.gift.GiftChannel;
import com.ncmmall.domain.business.marketing.gift.GiftGoods;
import com.ncmmall.domain.business.marketing.gift.GiftScope;
import com.ncmmall.domain.business.marketing.gift.QGiftActivity;
import com.ncmmall.domain.business.marketing.gift.QGiftChannel;
import com.ncmmall.domain.business.marketing.gift.QGiftGoods;
import com.ncmmall.domain.business.marketing.gift.QGiftScope;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 赠品活动管理
 *
 * @author linxs
 * @date 2020-01-17 23:13
 */
@Controller
@RequestMapping(value = "/mall/gift")
@Slf4j
public class GiftController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private GiftApplication giftApplication;

    @Autowired
    private OperationLogApplication logApplication;

    /**
     * 赠品活动管理
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private String list(Model model, PageParams pageParams, GiftActivity gift) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        String hql = builderSQL("select u from GiftActivity u where 1=1 ", params, gift);
        QueryPage<GiftActivity> page = queryChannelService.findPage(hql, pageRequest.setParams(params), GiftActivity.class);

        model.addAttribute("page", page).addAttribute("params", gift);

        return "mall/marketing/gift/list";
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
    private String detail(Model model, @PathVariable("id") long id) {

        GiftActivity giftActivity = queryChannelService.findOne(QGiftActivity.giftActivity.id.eq(id), GiftActivity.class);

        List<GiftGoods> giftGoodses = queryChannelService.findAll(QGiftGoods.giftGoods.gift.eq(giftActivity), GiftGoods.class);
        List<GiftChannel> giftChannels = queryChannelService.findAll(QGiftChannel.giftChannel.gift.eq(giftActivity), GiftChannel.class);
        List<GiftScope> giftScopes = queryChannelService.findAll(QGiftScope.giftScope.gift.eq(giftActivity), GiftScope.class);

        model.addAttribute("gift", giftActivity).addAttribute("giftGoodses", giftGoodses).addAttribute("channels", giftChannels).addAttribute("scopes", giftScopes);

        return "mall/marketing/gift/detail";

    }

    /**
     * 审核页面
     */
    @RequestMapping(value = "/{id}/process", method = RequestMethod.GET)
    private String process(Model model, @PathVariable("id") long id) {

        GiftActivity giftActivity = queryChannelService.findOne(QGiftActivity.giftActivity.id.eq(id), GiftActivity.class);

        List<GiftGoods> giftGoodses = queryChannelService.findAll(QGiftGoods.giftGoods.gift.eq(giftActivity), GiftGoods.class);
        List<GiftChannel> giftChannels = queryChannelService.findAll(QGiftChannel.giftChannel.gift.eq(giftActivity), GiftChannel.class);
        List<GiftScope> giftScopes = queryChannelService.findAll(QGiftScope.giftScope.gift.eq(giftActivity), GiftScope.class);

        model.addAttribute("gift", giftActivity).addAttribute("giftGoodses", giftGoodses).addAttribute("channels", giftChannels).addAttribute("scopes", giftScopes).addAttribute("_method", "PUT");

        return "mall/marketing/gift/process";
    }

    /**
     * 审核赠品活动
     */
    @RequestMapping(value = "/{id}/process", method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> process(HttpServletRequest request, GiftActivity params) {
        try {

            giftApplication.process(params);
            logApplication.eventOfSystem(Securitys.currentUser(), "审核赠品活动" + params.getName(), Securitys.getIp(request));
            return ImmutableMap.of("status", true, "message", "审核成功");

        } catch (Exception e) {
            log.error("[审核赠品活动异常]", e);
            return ImmutableMap.of("status", false, "message", "审核异常");
        }
    }


    /**
     * 条件筛选
     */
    private String builderSQL(String hql, Map<String, Object> params, GiftActivity gift) {
        if (gift.getStatus() == 0) {
            hql += " and u.status in (1,2,3,4,5) ";
        } else if (gift.getStatus() == 4) {
            hql += " and u.status in (1,4) ";
        } else {
            hql += " and u.status = :status ";
            params.put("status", gift.getStatus());
        }

        if (StringUtils.isNotBlank(gift.getBeginDateOfValidity())) {
            hql += " and u.beginDateOfValidity >= :start";
            params.put("start", gift.getBeginDateOfValidity());
        }

        if (StringUtils.isNotBlank(gift.getEndDateOfValidity())) {
            hql += " and u.endDateOfValidity <= :end";
            params.put("end", gift.getEndDateOfValidity());
        }

        return hql + " order by u.createTime desc";
    }

}
